package qst.com.bacK.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.ChoiceMapper;
import qst.com.bacK.mapper.DimensionMapper;
import qst.com.bacK.mapper.QuestionMapper;
import qst.com.bacK.pojo.entity.Choice;
import qst.com.bacK.pojo.entity.PersonalityDimension;
import qst.com.bacK.pojo.entity.Question;
import qst.com.bacK.question.QuestionQueryParam;
import qst.com.bacK.service.QuestionService;

import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/10 下午10:16
 * Description:
 */

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ChoiceMapper choiceMapper;
    @Autowired
    private DimensionMapper dimensionMapper;
    @Override
    public List<Question> find(QuestionQueryParam param) {
        return questionMapper.find(param);
    }

    @Override
    public List<Choice> findChoices(Integer id) {
        return questionMapper.findChoices(id);
    }

    @Override
    public Question findById(int id) {
        return questionMapper.findById(id);
    }

    @Override
    public void deleteById(int id) {
        questionMapper.deleteById(id);
    }

    @Override
    public List<Integer> findDimensionIdByQuestion(int qid) {
        return questionMapper.findDimension(qid);
    }

    @Override
    public void attachDimension(int qid, int[] pids) {
        questionMapper.detachDimension(qid);
        for (int p : pids) {
            questionMapper.attachDimension(qid, p);
        }
    }

    @Override
    public void save(Question q, List<Choice> choices) {
        isChoiceValid(q.getType(), choices);
        q.setStatus(2);
        // 这个insert方法里有获取考题id的逻辑
        questionMapper.insert(q);
        for (Choice ch : choices) {
            // 上面的insert方法里有获取考题id的逻辑
            ch.setQuestionId(q.getId());
            choiceMapper.insert(ch);
        }
    }

    @Override
    public void update(Question q, List<Choice> choices) {
        isChoiceValid(q.getType(), choices);
        questionMapper.update(q);
        for (Choice ch : choices) {
            ch.setQuestionId(q.getId());
            choiceMapper.update(ch);
        }
    }

    @Override
    public List<PersonalityDimension> findDimensionByQuestion(Integer id) {
        return dimensionMapper.findDimensionByQuestion(id);
    }

    private void isChoiceValid(int type, List<Choice> choices) {
        int count = 0;
        for (Choice ch : choices) {
            if (ch.isChecked()) {
                count++;
            }
        }
        if (count == 0) {
            throw new ExamException("请选择本题的正确选项");
        }
        if (type == 1 && count != 1) {
            throw new ExamException("单选题只能有一个正确选项");
        }
        if (type == 2 && count < 2) {
            throw new ExamException("多选题至少要有两个正确选项");
        }
    }
}
